<script>
export default {
  name: "TodoFooter",
  props:{
    taskList:{
      type:Array
    }
  },
  methods:{
    upIsAll(e){
      this.$emit("update:taskList",this.taskList.map(item=>{
        item.isChecked = e.target.checked;
        return item;
      }))
    },
    // 清除所有选中的任务数据
    clearCheckedTask(){
      // console.log("clearCheckedTask");
      this.$emit("update:taskList",this.taskList.filter(v=>!v.isChecked));
    }
  },
  computed:{
    overTaskNum(){
      return this.taskList.reduce(function(sum,item){
        if(item.isChecked) sum++;
        return sum;
      },0)
    },
    isAll(){
      return this.taskList.length>0 && this.taskList.length === this.overTaskNum;
    }
  }
}
</script>

<template>
  <div class="todo-footer">
    <label>
      <input @change="upIsAll" :checked="isAll" type="checkbox"/>
    </label>
    <span>已完成{{overTaskNum}} / 全部{{taskList.length}}</span>
    <button @click="clearCheckedTask" v-show="overTaskNum>0" class="btn btn-danger">清除已选中任务</button>
  </div>
</template>

<style scoped>
/*footer*/
.todo-footer {
  height: 40px;
  line-height: 40px;
  padding-left: 6px;
  margin-top: 5px;
}

.todo-footer label {
  display: inline-block;
  margin-right: 20px;
  cursor: pointer;
}

.todo-footer label input {
  position: relative;
  top: -1px;
  vertical-align: middle;
  margin-right: 5px;
}

.todo-footer button {
  float: right;
  margin-top: 5px;
}
</style>